package cn.findviewbydream.evil.bigtoutiao.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;

import butterknife.BindView;
import cn.findviewbydream.evil.bigtoutiao.R;
import cn.findviewbydream.evil.bigtoutiao.base.BaseActivity;
import cn.findviewbydream.evil.bigtoutiao.fragment.IndexFragment;
import cn.findviewbydream.evil.bigtoutiao.fragment.MicroFragment;
import cn.findviewbydream.evil.bigtoutiao.fragment.VideoFragment;

public class MainActivity extends BaseActivity
{
    @BindView(R.id.fragment)
    FrameLayout fragment;

    @BindView(R.id.bottomNavigationBar)
    BottomNavigationBar bottomNavigationBar;

    private static final Fragment[] FRAGMENTS = {
            IndexFragment.getInstance(),
            VideoFragment.getInstance(),
            MicroFragment.getInstance(),
            MicroFragment.getInstance()
    };

    private static int lastSelectedPosition = 0;
    private static Fragment presentFragment;

    private TextBadgeItem badgeItem;

    @Override
    public int setContentViewId()
    {
        return R.layout.activity_main;
    }

    @Override
    public void onActivityCreate(Bundle savedInstanceState)
    {
        initView();
    }

    @Override
    protected void initView()
    {
        initFragment();
        initBottomNavigationBar();
    }

    private void initBottomNavigationBar()
    {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setBarBackgroundColor(R.color.b_white);
        bottomNavigationBar.setInActiveColor(R.color.p_gray);

        badgeItem = new TextBadgeItem().setText("15");

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.index, "首页")
                        .setActiveColorResource(R.color.colorPrimary)
                        .setBadgeItem(badgeItem))
                .addItem(new BottomNavigationItem(R.drawable.video, "视频")
                        .setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.micro, "微头条")
                        .setActiveColorResource(R.color.colorPrimary))
                .addItem(new BottomNavigationItem(R.drawable.user, "我的")
                        .setActiveColorResource(R.color.colorPrimary))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();


        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener()
        {
            @Override
            public void onTabSelected(int position)
            {
                lastSelectedPosition = position;
                selectFragment(presentFragment, FRAGMENTS[position]);
            }

            @Override
            public void onTabUnselected(int position)
            {

            }

            @Override
            public void onTabReselected(int position)
            {
                if (position == 0 && !badgeItem.isHidden())
                    badgeItem.hide();
            }
        });
    }

    private void initFragment()
    {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.fragment, FRAGMENTS[lastSelectedPosition]);
        transaction.commit();

        presentFragment = FRAGMENTS[lastSelectedPosition];
    }

    private void selectFragment(Fragment from, Fragment to)
    {
        if (from != to)
        {
            presentFragment = to;
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);

            if (!to.isAdded())
                // 先判断是否被add过
                transaction.hide(from).add(R.id.fragment, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            else
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
        }
    }

}
